(function() {
	require(["/js/tools/getarea.js"])();
	coAjax.get(appConfig.user.address_detail, function(result) {
		App.set("$Cache.user.address.detail", result.result);
	});
	// 增加
	App.set("$Event.user.address.add_user_address", function() {
		var new_user_address = {
			province: $("[name='province']").find('option:selected').text(),
			town: $("[name='city']").val(),
			county: $("[name='area']").val(),
			// street:
			detail: $("[name='detailed_addre']").val(),
			mobilenumber: $("[name='mobile_num']").val(),
			phonenumber: $("[name='phone_num']").val(),
			postcode: $("[name='zip_code']").val(),
			name: $("[name='name']").val()
		};
		console.log(new_user_address);
		coAjax.post(appConfig.user.add_address, new_user_address, function(result) {
			var address_detail = App.get("$Cache.user.address.detail");
			address_detail.unshift(result.result);
			App.set("$Cache.user.address.detail", address_detail);
			alert("success", "添加地址成功！");
			coAjax.put(appConfig.user.set_default_address, {
				address_id: result.result._id
			}, function(result) {
				App.set("loginer.default_ua_id", result.result);
				App.getModel().touchOff("$Cache.user.address.detail");
				alert("success", "已将新添加地址修改为默认地址！");
			});
		}, function(errorCode, xhr, errorMsg) {
			alert("error", errorMsg);
		});
	}, function(errorCode, xhr, errorMsg) {
		alert("error", errorMsg);
	});
	// 修改默认
	App.set("$Event.user.address.set_default_address", function(e, vm) {
		coAjax.put(appConfig.user.set_default_address, {
			address_id: vm.get("_id")
		}, function(result) {
			App.set("loginer.default_ua_id", result.result);
			App.getModel().touchOff("$Cache.user.address.detail");
			alert("success", "修改默认地址成功！");
		});
	}, function(errorCode, xhr, errorMsg) {
		alert("error", errorMsg);
	});
	App.set("$Event.user.address.remove_address", function(e, vm) {
		coAjax["delete"](appConfig.user.remove_address, {
			address_id: vm.get("_id")
		}, function(result) {
			App.set("$Cache.user.address.detail", result.result);
		});
	}, function(errorCode, xhr, errorMsg) {
		alert("error", errorMsg);
	});
}());